package com.zhongying.jf.util;

import com.zhongying.jf.model.BaseRequestInfo;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class WebContext {

	private static ThreadLocal<HttpServletRequest> _request = new ThreadLocal<HttpServletRequest>();
	
	private static ThreadLocal<HttpServletResponse> _response = new ThreadLocal<HttpServletResponse>();

	private static ThreadLocal<ServletContext> _servletContext = new ThreadLocal<ServletContext>();
	
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = _request.get();
		return request;
	}

	public static void setRequest(HttpServletRequest request) {
		_request.set(request);
	}

	public static HttpServletResponse getResponse() {
		return _response.get();
	}
	
	public static void setResponse(HttpServletResponse response) {
		_response.set(response);
	}

	public static ServletContext getServletContext() {
		
		return _servletContext.get();
	}

	public static void setServletContext(ServletContext servletContext) {
		_servletContext.set(servletContext);
	}


	public static BaseRequestInfo getBaseInfo() {
		HttpServletRequest request = getRequest();
		String sourceUrl =  request.getHeader("Referer");
		String currentUrl = request.getRequestURI();

		String ip = WebUtil.getIpAddr();

		BaseRequestInfo baseRequestInfo = new BaseRequestInfo();
		baseRequestInfo.setRemote_ip(ip);
		baseRequestInfo.setTerminalSouce(sourceUrl);
		return baseRequestInfo;
	}
}
